package com.jack.blog.controller;

import com.jack.blog.cache.BlogServiceCache;
import com.jack.blog.entity.Blog;
import com.jack.blog.entity.Message;
import com.jack.blog.service.BlogService;
import com.jack.blog.vo.BlogVo;
import com.jack.blog.vo.info.BlogInfo;
import com.jack.blog.vo.MessageVo;
import com.jack.blog.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user/{uid}")
public class BlogController {
    @Autowired
    private BlogServiceCache blogServiceCache;

    @PostMapping("/blog")
    public String addBlog(@PathVariable("uid")int uid, BlogInfo blogInfo, Model model){
        String url="/user/"+uid+"/blogs";
        try {
            blogServiceCache.addBlog(uid, blogInfo);
            return "redirect:"+url;
        }catch (IllegalArgumentException i){
            i.printStackTrace();
            model.addAttribute("error",i.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","add error!");
        }
        model.addAttribute("backUrl",url);
        return "error";
    }

    @GetMapping("/blog/{bid}")
    public String getblog(@PathVariable("bid") int bid){
        BlogVo blog = blogServiceCache.getBlog(bid);
        String url="blogs/"+blog.getHtml();
        return url;
    }

    @PostMapping("/blog/{bid}/update")
    public String updateBlog(@PathVariable("uid")int uid, @PathVariable("bid") int bid, BlogInfo blogInfo, Model model){
        String url="/user/"+uid+"/blogs";
        try {
            blogServiceCache.updateBlogInfo(uid,bid, blogInfo);
            return "redirect:"+url;
        }catch (IllegalArgumentException i){
            model.addAttribute("error",i.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","update error!");
        }
        model.addAttribute("backUrl",url);
        return "error";
    }

    @GetMapping("/blog/{bid}/delete")
    public String deleteBlog(@PathVariable("uid")int uid, @PathVariable("bid") int bid, Model model){
        String url="/user/"+uid+"/blogs";
        try {
            blogServiceCache.deleteBlog(uid,bid);
            return "redirect:"+url;
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","delete error!");
            model.addAttribute("backUrl",url);
            return "error";
        }
    }

    @GetMapping("/blog/{bid}/good")
    @ResponseBody
    public Response good(@PathVariable("uid")int uid,@PathVariable("bid") int bid){
        Response response=new Response();
        try {
            blogServiceCache.addGood(uid,bid);
            response.setMsg("别这样!");
            response.setStatus(200);
        }catch (Exception e){
            e.printStackTrace();
            response.setMsg("出错洛!");
            response.setStatus(500);
        }
        return response;
    }

    @GetMapping("/blog/{bid}/bad")
    @ResponseBody
    public Response bad(@PathVariable("uid")int uid,@PathVariable("bid") int bid){
        Response response=new Response();
        try {
            blogServiceCache.addBad(uid,bid);
            response.setMsg("感谢!");
            response.setStatus(200);
        }catch (Exception e){
            e.printStackTrace();
            response.setMsg("出错洛!");
            response.setStatus(500);
        }
        return response;
    }

}
